package cn.zyx.pay.service.impl;

import cn.zyx.basic.exception.BusinessException;
import cn.zyx.basic.service.impl.BaseServiceImpl;
import cn.zyx.order.service.IProductOrderService;
import cn.zyx.pay.domain.AlipayInfo;
import cn.zyx.pay.domain.PayBill;
import cn.zyx.pay.mapper.AlipayInfoMapper;
import cn.zyx.pay.mapper.PayBillMapper;
import cn.zyx.pay.service.IPayBillService;
import cn.zyx.pay.utils.AlipayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class PayBillServiceImpl extends BaseServiceImpl<PayBill> implements IPayBillService {

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private IProductOrderService productOrderService;

    @Override
    public String pay(PayBill payBill) {
        // 判断传过来的支付单是否真实
        if(payBill == null){
            throw new BusinessException("请生成支付单后，再完成支付!!!");
        }
        // 通过当前支付单的唯一支付标识查询支付单
        PayBill bill = payBillMapper.loadByUnionPaySn(payBill.getUnionPaySn());
        if (bill == null){
            throw new BusinessException("请生成支付单后，再完成支付!!!");
        }
        // 获取订单的支付渠道
        Long payChannel = bill.getPayChannel();
        // switch选择结构选择支付方式
        String resultData = "";
        switch(payChannel.intValue()){
            case 1 :    // 支付宝
                System.out.println("--------------------"+bill.getShop_id());
                AlipayInfo alipayInfo = alipayInfoMapper.loadByShopId(bill.getShop_id());   // 根据账单的商店id获取支付宝支付的信息
                resultData = AlipayUtil.pay(alipayInfo, bill);  // 调用工具类获取支付宝form表单 -- 支付页面
                break;
            case 2 :    // 微信
                //@TODO
                break;
            case 3 :    // 银联
                //@TODO
                break;
            //你可以有任意数量的case语句
            default :   // 余额
                //@TODO
        }
        return resultData;
    }

    @Override
    public PayBill loadByUnionPaySn(String unionPaySn) {
        return payBillMapper.loadByUnionPaySn(unionPaySn);
    }
}
